﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Web.UI.WebControls;
using Org.Eurekaa.DNJ.UI.DNJSettings;
using System.Web.UI;
using Org.Eurekaa.DNJ.Settings;
using Org.Eurekaa.DNJ.Helpers;
using System.Web.UI.HtmlControls;

namespace Org.Eurekaa.DNJ.UI.WebControls
{
    [ToolboxBitmap(typeof(Org.Eurekaa.DNJ.UI.Resources.icons.DNJUIIcons), "DNJAutocomplete.bmp")]
    [Designer(typeof(Org.Eurekaa.DNJ.UI.Design.WebControls.JQueryUIControlDesigner))]
    [DefaultProperty("Settings")]
    [ToolboxData("<{0}:DNJAutoComplete runat=server Width=150px></{0}:DNJAutoComplete>")]
    public class DNJAutoComplete : JQueryUIControl
    {        
        private TextBox _txtBox = new TextBox();

        private DNJAutoCompleteSettings _settings = new DNJAutoCompleteSettings();
        [DefaultValue(""), Category("$.DNJ Settings"), DisplayName("Settings")]
        [PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DNJAutoCompleteSettings Settings
        {
            get { return _settings; }
            set { _settings = value; }
        }

        private DNJAutoCompleteEvents _events = new DNJAutoCompleteEvents();
        [DefaultValue(""), Category("$.DNJ Settings"), DisplayName("Events")]
        [PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DNJAutoCompleteEvents ClientEvents
        {
            get { return _events; }
            set { _events = value; }
        }


        [DefaultValue(""), Category("$.DNJ Settings")]
        public string Text
        {            
            get { return _txtBox.Text; }
            set { _txtBox.Text = value; }
        }

        [DefaultValue(150)]
        public override Unit Width
        {
            get { return _txtBox.Width; }
            set { _txtBox.Width = value; }
        }


        public override void SaveSettings(ref object stateObject)
        {            
            object[] lstObj = new object[2];
            lstObj[0] = Settings;
            lstObj[1] = ClientEvents;

            stateObject = lstObj;
            
            GenericSettingsObject allSettings = new GenericSettingsObject();

            PageHelper.AppendJQueryScript(ClientEvents.GetDefaultEventsScript(), this.Page.Header);

            Settings.PrepareJSON();
            ClientEvents.PrepareJSON();
            allSettings.Append(Settings);
            allSettings.Append(ClientEvents);

            PageHelper.AppendJQueryPlugin("#" + _txtBox.ClientID, "autocomplete", allSettings.ToString(), this.Page.Header);

        }


        public override void LoadSettings(object stateObject)
        {
            object[] lstObj = (object[])stateObject;
            Settings = (lstObj[0] as DNJAutoCompleteSettings);
            ClientEvents = (lstObj[1] as DNJAutoCompleteEvents);
        }

        
        
        protected override void Render(HtmlTextWriter writer)
        {            
            if (this.DesignMode)
            {                
                _txtBox.Attributes.Add("style", "width:" + this.Width.ToString());
                _txtBox.RenderControl(writer);                
            }
            else
                base.Render(writer);
        }

        protected override void CreateChildControls()
        {
            //Controls.Clear();
            _txtBox.ID = "acfield_" + this.ID;
            
            if (DesignMode)
            {
                HtmlGenericControl ctrlDiv = new HtmlGenericControl("div");
                ctrlDiv.Controls.Add(_txtBox);

                Controls.Add(ctrlDiv);
            }
            else
            {
                Controls.Add(_txtBox);
            }

            base.CreateChildControls();

        }
    }


}
